import { pathOr } from 'ramda';

const getCount = pathOr(0, [0, 'count']);

const fields = [
	{
		path: ['balance', 'domain'],
		query: ({ model, balance }) =>
			model.Job.countDocuments({
				uid: balance.uid,
			}),
	},
	{
		path: ['balance', 'domainEnable'],
		query: ({ model, balance }) =>
			model.Job.countDocuments({
				uid: balance.uid,
				enableMonitor: true,
				'verification.isVerified': true,
			}),
	},
	{
		path: ['balance', 'httpMaxTask'],
		query: ({ model, balance }) =>
			model.Job.aggregate([
				{
					$match: {
						uid: balance.uid,
						'verification.isVerified': true,
					},
				},
				{
					$group: { _id: 'http', count: { $sum: { $size: '$httpSettings' } } },
				},
			]).then(x => getCount(x)),
	},
	{
		path: ['balance', 'pingMaxTask'],
		query: ({ model, balance }) =>
			model.Job.aggregate([
				{
					$match: {
						uid: balance.uid,
						'verification.isVerified': true,
					},
				},
				{
					$group: { _id: 'ping', count: { $sum: { $size: '$pingSettings' } } },
				},
			]).then(x => getCount(x)),
	},
	{
		path: ['balance', 'httpMaxTaskEnable'],
		query: ({ model, balance }) =>
			model.Job.aggregate([
				{
					$match: {
						uid: balance.uid,
						enableMonitor: true,
						'verification.isVerified': true,
					},
				},
				{ $unwind: '$httpSettings' },
				{ $match: { 'httpSettings.enable': true } },
				{
					$group: { _id: 'httpEnable', count: { $sum: 1 } },
				},
			]).then(x => getCount(x)),
	},
	{
		path: ['balance', 'pingMaxTaskEnable'],
		query: ({ model, balance }) =>
			model.Job.aggregate([
				{
					$match: {
						uid: balance.uid,
						enableMonitor: true,
						'verification.isVerified': true,
					},
				},
				{ $unwind: '$pingSettings' },
				{ $match: { 'pingSettings.enable': true } },
				{
					$group: { _id: 'pingEnable', count: { $sum: 1 } },
				},
			]).then(x => getCount(x)),
	},
	{
		path: ['balance', 'sitePortrait'],
		query: ({ model, balance }) =>
			model.Job.aggregate([
				{
					$match: {
						uid: balance.uid,
						enableMonitor: true,
						'verification.isVerified': true,
						'vulSettings.enable': true,
						'vulSettings.enableSitePortraitTriggerMonitor': true,
					},
				},
				{
					$group: {
						_id: 'enableSitePortraitTriggerMonitor',
						count: { $sum: 1 },
					},
				},
			]).then(x => getCount(x)),
	},
];

export default async function balanceTaskCountUpdate(uid) {
	const {
		app: { model },
	} = this;

	const balance = await model.Balance.findOne({ uid });
	this.log.info({ uid }, '更新套餐任务统计数量');

	const actions = {};
	for (const { path, query } of fields) {
		const field = pathOr(null, path, balance);
		const count = await query({ model, balance });

		if (!Number.isInteger(count)) continue;

		actions[path.concat('used').join('.')] = count;

		if (!field.charges || !field.charges.length) continue;

		// TODO: 需要更具充值情况具体判断
		actions[path.concat(['charges', 0, 'used']).join('.')] = count;
	}

	return model.Balance.updateOne({ uid }, { $set: actions });
}
